package br.com.netomarin.bundesliga.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.netomarin.bundesliga.model.PlayerModel;
import br.com.netomarin.bundesliga.persistence.Player;


public class RankingServlet extends HttpServlet {

	private static final long serialVersionUID = -4528707442359927390L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		List<Player> ranking = PlayerModel.getPlayersToRanking();
		Iterator<Player> it = ranking.iterator();
		
		Player p = null;
		PrintWriter w = resp.getWriter();
		w.print("<HTML><HEAD><TITLE>Ranking Bundesliga @ BR-SAO</TITLE></HEAD><BODY>");
		w.print("<h1>Ranking Bundesliga - BR-SAO</h1>");
		w.print("<table border=1><tr></tr>");
		w.print("<th>#</th>");
		w.print("<th>Nome</th>");
		w.print("<th>Pontos</th>");
		w.print("<th>Jogos</th>");
		w.print("<th>M&eacute;dia</th>");
		w.print("<th>V</th>");
		w.print("<th>D</th>");
		w.print("<th>E</th>");
		w.print("<th>Gols Pr&oacute;</th>");
		w.print("<th>Gols Contra</th>");
		w.print("<th>Saldo</th>");
		w.print("</tr>");
		for (int i = 1; it.hasNext(); i++) {
			p = it.next();
			w.print("<tr>");
			w.print("<td>"+i+"</td>");
			w.print("<td>"+p.getName()+"</td>");
			w.print("<td>"+p.getPoints()+"</td>");
			w.print("<td>"+p.getGames()+"</td>");
			w.print("<td>"+p.getAverage()+"</td>");
			w.print("<td>"+ p.getWins() +"</td>");
			w.print("<td>"+ p.getLosses() +"</td>");
			w.print("<td>"+ p.getDraws() +"</td>");
			w.print("<td>"+p.getGoalsScored()+"</td>");
			w.print("<td>"+p.getGoalsConceded()+"</td>");
			w.print("<td>"+(p.getGoalsScored()-p.getGoalsConceded())+"</td>");
			w.print("</tr>");
		}
		w.print("</table></body></html>");
	}
}